#region Using Statements
using System;
using System.IO;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
#endregion

namespace SkyBurner
{
    public enum GUIButtons
    {
        btOK,
        btCancel
    }

    public interface IGUIManagerEvent
    {
        void OnGUIPopupClick(Object pSender, GUIButtons pButton);
    }

    // GUIPopup class
    //
    #region GUIPopup class
    public class GUIPopup : BaseObject
    {
        protected Texture2D m_popupBackground;
        protected Rectangle m_sourceRect;
        protected Rectangle m_destRect;
        protected GUIButtons m_guiButtons;
        protected String[] m_splittedMessage;
        protected IGUIManagerEvent m_IGUIManagerEvent;

        public GUIPopup(BaseGame pGame, Vector2 pPosition, Rectangle pRectSize, Texture2D pBackground, IGUIManagerEvent pIGUIManagerEvent)
            : base(pGame)
        {
            m_IGUIManagerEvent = pIGUIManagerEvent;
            m_popupBackground = pBackground;
            m_sourceRect = new Rectangle(0, 0, pBackground.Width, pBackground.Height);
            m_destRect = new Rectangle((int)pPosition.X, (int)pPosition.Y, pRectSize.Width, pRectSize.Height);
        }

        public virtual void Update(GameTime gameTime)
        {
            if (m_Game.UserInputState.IsUserAction(UserAction.ButtonA))
                m_IGUIManagerEvent.OnGUIPopupClick(this, GUIButtons.btOK);
            else if (m_Game.UserInputState.IsUserAction(UserAction.ButtonB))
                m_IGUIManagerEvent.OnGUIPopupClick(this, GUIButtons.btCancel);
        }

        public virtual void Draw(GameTime gameTime, SpriteBatch pSpriteBatch)
        {
            // draw popup...
            pSpriteBatch.Begin();
            pSpriteBatch.Draw(m_popupBackground, m_destRect, m_sourceRect, Color.White);
            pSpriteBatch.End();
        }
    }
    #endregion

    // GUIManager class
    //
    #region GUIManager class
    public class GUIManager : BaseObject
    {
        private PostScreenEffectBase m_screenBlurShader;

        protected Collection<GUIPopup> m_popupList;
                 
        public bool IsPopupVisible
        {
            get { return m_popupList.Count > 0; }
        }

        public GUIManager(BaseGame pGame)
            : base(pGame)
        {
            m_popupList = new Collection<GUIPopup>();
            m_screenBlurShader = (PostScreenEffectBase)new PostScreenEffectBlur(m_Game, 1.0f);
        }

        public void Update(GameTime gameTime)
        {
            if (m_popupList.Count > 0)
                m_popupList[m_popupList.Count - 1].Update(gameTime);
        }

        public void Draw(GameTime gameTime)
        {
            foreach (GUIPopup popupItem in m_popupList)
                popupItem.Draw(gameTime, m_Game.SpriteBatch);
        }

        public void ShowPopup(Vector2 pPosition, Rectangle pRectSize, Texture2D pPopupTexture, IGUIManagerEvent pIGUIManagerEvent)
        {
            if (m_popupList.Count == 0)
                m_Game.PostScreenEffectsManager.AddPostScreenEffect(m_screenBlurShader);

            ShowPopup(new GUIPopup(m_Game, pPosition, pRectSize, pPopupTexture, pIGUIManagerEvent));
        }

        public void ShowPopup(GUIPopup pGUIPopup)
        {
            m_popupList.Add(pGUIPopup);
        }
    
        public void ClosePopup()
        {
            m_popupList.RemoveAt(m_popupList.Count - 1);

            if (m_popupList.Count == 0)
                m_Game.PostScreenEffectsManager.RemovePostScreenEffect(m_screenBlurShader);
        }
    }
    #endregion
}

